package com.example.wwk.graduationpractice.AsyncTask;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;

import com.example.wwk.graduationpractice.Globalutils.CustomDialogUtils;
import com.example.wwk.graduationpractice.Globalutils.PromptShowUtils;
import com.example.wwk.graduationpractice.NetDataUtils.GroupUtils;
import com.example.wwk.graduationpractice.R;
import com.example.wwk.graduationpractice.SmackUtils.XmppConnectUtils;

import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.chat.Chat;
import org.jivesoftware.smack.chat.ChatManager;
import org.jivesoftware.smack.packet.Presence;

import java.io.IOException;

/**
 * Created by wwk on 2017/4/29.
 */

public class FindPwdAsyncTask {
    public static final int findPwdOk = 744;
    public static final int findPwdOff = 745;
    /*
    注册按钮事件
    */
    public static void findPwd(final String email, final Handler handler) {
        new AsyncTask<String, String, Boolean>() {//传入任务的参数类型；显示进度的单位；返回结果类型
            private AbstractXMPPConnection connection;     //获取连接
            private ChatManager chatManager;
            private Chat chat;
            private String content = email;
            private String emailReceiver = "email";
            private String testUser = "test";
            private String testPwd = "123";

            protected void onPreExecute() {              //任务开始前执行，一些初始化操作

            }

            @Override
            protected Boolean doInBackground(String... strings) {
                connection = XmppConnectUtils.getConnection();
                try {
                    connection.login(testUser, testPwd);
                } catch (XMPPException e) {
                    e.printStackTrace();
                } catch (SmackException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                chatManager = ChatManager.getInstanceFor(connection);      //获取实例
                chat = chatManager.createChat(emailReceiver + "@" + connection.getServiceName());   //为发送信息(email)建立聊天通道
                try {
                    chat.sendMessage(content);
                } catch (SmackException.NotConnectedException e) {
                    e.printStackTrace();
                    return false;
                }
                return true;

            }

            @Override
            protected void onPostExecute(Boolean bool) {
                Presence presence = new Presence(Presence.Type.available);
                try {
                    connection.disconnect(presence);
                    connection = null;
                } catch (SmackException.NotConnectedException e) {
                    e.printStackTrace();
                }
                Message message = new Message();
                if (bool.equals(true)) {
                    message.what = findPwdOk;
                } else {
                    message.what = findPwdOff;
                }
                handler.sendMessage(message);
            }
        }.execute();
    }
}
